﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace BOMSWinService.Control
{
    public partial class UCInputIP : UserControl
    {
        public UCInputIP()
        {
            InitializeComponent();
        }
        public string IP
        {
            get {
                return tbxIPSec1.Text.Trim() + "."
                    + tbxIPSec2.Text.Trim() + "."
                    + tbxIPSec3.Text.Trim() + "."
                    + tbxIPSec4.Text.Trim();
            }
            set { 
                string[] arr = value.Split('.');
                tbxIPSec1.Text = arr[0];
                tbxIPSec2.Text = arr[1];
                tbxIPSec3.Text = arr[2];
                tbxIPSec4.Text = arr[3];
            }
            
        }
        public int Port
        {
            get
            {
                return int.Parse(tbxPort.Text.Trim());
            }
            set {
                tbxPort.Text = Port.ToString();
            }

        }
        public bool IsChecked
        {
            get
            {
                return checkBox1.Checked;
            }
        }

        public bool IsInputValid(out string Message)
        {           
           
            if (!(CheckIPSec(tbxIPSec1, out Message)
                && CheckIPSec(tbxIPSec2, out Message)
                && CheckIPSec(tbxIPSec3, out Message)
                && CheckIPSec(tbxIPSec4, out Message)
                && CheckPortSec(tbxPort,out Message)
                ))
            {
                return false;
            }
            return true;
        }
        public void Delete()
        {
            this.Dispose();            
        }
        private bool CheckIPSec(TextBox tbx,out string Result)
        {
            string inputIPSec = tbx.Text; 
            Result = "OK";
            int n ;
            if (int.TryParse(inputIPSec, out n))
            {
                if (n < 0 || n > 255)
                {
                    Result = "IP数值必须0~255之间";
                    tbx.Select(0, tbx.Text.Length);
                    return false;
                }

            }else{
                Result = "必须填入数值";
                tbx.Select(0, tbx.Text.Length);
                return false;
            }
            return true;
        }
        private bool CheckPortSec(TextBox tbx, out string Result)
        {
            string inputIPSec = tbx.Text;
            Result = "OK";
            int n;
            if (int.TryParse(inputIPSec, out n))
            {
                if (n < 0 || n > 65535)
                {
                    Result = "端口数值必须0 ~65535之间";
                    tbx.Select(0, tbx.Text.Length);
                    return false;
                }
            }
            else
            {
                Result = "必须填入数值";
                tbx.Select(0, tbx.Text.Length);
                return false;
            }
            return true;
        }
        private void tbxIPSec1_KeyPress(object sender, KeyPressEventArgs e)
        {
               //保证输入为数字
            if ("0123456789".IndexOf(e.KeyChar) == -1 
                && e.KeyChar != (char)9
                 && e.KeyChar != (char)8
                )
            {
                e.Handled = true;
            }
            else
            {
                e.Handled = false;
            }
        }
    }
}
